package org.tinygroup.bizframe.business.impl;

import java.util.ArrayList;
import java.util.List;

import org.tinygroup.bizframe.business.inter.SysRoleRightBusiness;
import org.tinygroup.bizframe.dao.inter.pojo.complex.TRightRef;
import org.tinygroup.bizframe.ext.dao.inter.TRoleRightRefDealDao;
import org.tinygroup.bizframe.ext.dao.inter.TsysRoleRightDao;
import org.tinygroup.bizframe.ext.dao.inter.pojo.TsysRoleRight;
import org.tinygroup.jdbctemplatedslsession.daosupport.OrderBy;
import org.tinygroup.menuframe.config.Menu;
import org.tinygroup.menuframe.dto.MenuInfo;
import org.tinygroup.menuframe.dto.MenuNode;
import org.tinygroup.menuframe.manager.MenuTreeManager;
import org.tinygroup.menuframe.util.MenuUtil;
import org.tinygroup.tinysqldsl.Pager;

public class SysRoleRightBusinessImpl implements SysRoleRightBusiness {
	
	private MenuTreeManager menuTreeManager;
	
	private TsysRoleRightDao tsysRoleRightDao;

	private TRoleRightRefDealDao tRoleRightRefDealDao;

	public TsysRoleRightDao getTsysRoleRightDao() {
		return tsysRoleRightDao;
	}

	public void setTsysRoleRightDao(TsysRoleRightDao tsysRoleRightDao) {
		this.tsysRoleRightDao = tsysRoleRightDao;
	}

	public TRoleRightRefDealDao gettRoleRightRefDealDao() {
		return tRoleRightRefDealDao;
	}

	public void settRoleRightRefDealDao(TRoleRightRefDealDao tRoleRightRefDealDao) {
		this.tRoleRightRefDealDao = tRoleRightRefDealDao;
	}

	public TsysRoleRight getById(Integer id) {
		return tsysRoleRightDao.getByKey(id);
	}

	public Pager<TsysRoleRight> getPager(int start, int limit,
			TsysRoleRight sysRoleRight, OrderBy... orderBies) {
		return tsysRoleRightDao.queryPager(start, limit, sysRoleRight, orderBies);
	}

	public TsysRoleRight add(TsysRoleRight sysRoleRight) {
		return tsysRoleRightDao.add(sysRoleRight);
	}

	public int update(TsysRoleRight sysRoleRight) {
		return tsysRoleRightDao.edit(sysRoleRight);
	}

	public int deleteByKeys(Integer... pks) {
		return tsysRoleRightDao.deleteByKeys(pks);
	}

	public boolean checkExists(TsysRoleRight sysRoleRight) {
		return tsysRoleRightDao.query(sysRoleRight).size() == 0 ? false : true;
	}

	public Pager queryRightsInRole(int start, int pageSize, Integer roleId, TRightRef tRightRef, Boolean isAssigned) {
		TsysRoleRight roleRight=new TsysRoleRight();
		roleRight.setRoleId(roleId);
		List<TsysRoleRight> lsRoleRight = tsysRoleRightDao.query(roleRight);
		List<Integer> lsMenuId= new ArrayList<Integer>();
		for(TsysRoleRight r : lsRoleRight){
			lsMenuId.add(r.getMenuId());
		}
		List<MenuNode> authed = menuTreeManager.getAuthMenuNodeList(lsMenuId);
		List<Menu> assigned=null;
		if(null != authed && !authed.isEmpty()){
			MenuInfo menuInfo = new MenuInfo();
	        menuInfo.setMenuList(authed);
			assigned = MenuUtil.toList(menuInfo);
			
		}
		else {
			assigned=new ArrayList<Menu>();
		}
		
		List<TRightRef> lsRight=new ArrayList<TRightRef>();
		
		if(isAssigned){
		   for(Menu m : assigned){
			   for(Integer mId : lsMenuId){
				   if(mId.equals(m.getId())){
					   TRightRef r=new TRightRef();
					   r.setMenuId(m.getId());
					   r.setKindCode(m.getKindCode());
					   r.setModelCode(m.getMenuType());
					   r.setMenuName(m.getMenuName());
					   lsRight.add(r); 
				   }
			   }
			   
		   }
		   	
		   Pager<TRightRef> pagerAssigned =new Pager<TRightRef>(assigned.size(),start,lsRight);
		   return pagerAssigned;
		}
		else{
			List<Menu> all = menuTreeManager.getAllMenu(null);
			for(Menu a : all){
				boolean hasAssigned=false;
				for(Integer mId : lsMenuId){
				   if(mId.equals(a.getId())){
					   hasAssigned=true;
				   }
				}
				if(!hasAssigned){
					TRightRef r=new TRightRef();
					r.setMenuId(a.getId());
					r.setMenuName(a.getMenuName());
					r.setKindCode(a.getKindCode());
					r.setModelCode(a.getMenuType());
					lsRight.add(r);
				}
			}
			 Pager<TRightRef> pagerNoAssigned =new Pager<TRightRef>(assigned.size(),start,lsRight);
			 return pagerNoAssigned;
		}
		//return tRoleRightRefDealDao.queryRightsInRole(start,10000,roleId,tRightRef,isAssigned);
	}

	public boolean grantRights(Integer roleId,Integer[] menuIds, String createBy) {
		tRoleRightRefDealDao.grantRights(roleId,menuIds, createBy);
		return true;
	}

	public boolean revokeRights(Integer roleId,Integer[] roleRightIds){
		int count = tRoleRightRefDealDao.revokeRights(roleId,roleRightIds);
		if(count > 0){
			return true;
		}else{
			return false;
		}
	}

	public List<Integer> queryAssignedRightsInRole(Integer roleId) {
		TsysRoleRight roleRight=new TsysRoleRight();
		roleRight.setRoleId(roleId);
		List<TsysRoleRight> lsRoleRight = tsysRoleRightDao.query(roleRight);
		List<Integer> lsMenuId= new ArrayList<Integer>();
		for(TsysRoleRight r : lsRoleRight){
			lsMenuId.add(r.getMenuId());
		}
		return lsMenuId;
	}

	public MenuTreeManager getMenuTreeManager() {
		return menuTreeManager;
	}

	public void setMenuTreeManager(MenuTreeManager menuTreeManager) {
		this.menuTreeManager = menuTreeManager;
	}

}
